package com.settlement.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.mapper.SysAccessoryMapper;
import com.settlement.system.model.entity.SysAccessory;
import com.settlement.system.model.form.AccessoryForm;
import com.settlement.system.model.query.AccessoryPageQuery;
import com.settlement.system.service.SysAccessoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 辅助项设置 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2023-09-07
 */
@Service
@RequiredArgsConstructor
public class SysAccessoryServiceImpl extends ServiceImpl<SysAccessoryMapper, SysAccessory> implements SysAccessoryService {

    @Override
    public Page<SysAccessory> getAccessoryPage(AccessoryPageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();
        // 查询数据
        Page<SysAccessory> accessoryPage = this.page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<SysAccessory>().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryCode1, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryName1, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryCode2, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryName2, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryCode3, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryName3, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryCode4, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryName4, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryCode5, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryName5, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryCode6, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryName6, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryCode7, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryName7, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryCode8, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryName8, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryCode9, keywords).or().like(StrUtil.isNotBlank(keywords), SysAccessory::getAccessoryName9, keywords).orderByDesc(SysAccessory::getUpdateTime));
        return accessoryPage;
    }

    @Override
    public boolean saveAccessory(AccessoryForm accessoryForm) {
        if (accessoryForm.getId() == null) {
            Assert.isTrue(this.list().size() == 0, "存在辅助项配置，不允许新增多条");
        } else {
            Assert.isTrue(this.list(new LambdaQueryWrapper<SysAccessory>().ne(SysAccessory::getId, accessoryForm.getId())).size() == 0, "存在辅助项配置，不允许新增多条");
        }
        SysAccessory sysAccessory = new SysAccessory();
        BeanUtil.copyProperties(accessoryForm, sysAccessory);
        return this.saveOrUpdate(sysAccessory);
    }

    @Override
    public SysAccessory getAccessoryFormData(Long accessoryId) {
        return this.getById(accessoryId);
    }

    @Override
    public boolean deleteAccessory(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的辅助项数据为空");
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public List<SysAccessory> findAll() {
        return this.list();
    }
}
